//
//  YMCustomTabBar.m
//  YMMedical
//
//  Created by y s on 2023/6/19.
//

#import "YMCustomTabBar.h"

@interface YMCustomTabBar ()

@property (nonatomic, strong) UIImageView *imgvBg;

@property (nonatomic, strong) UIView *vVip;

@end

@implementation YMCustomTabBar

- (instancetype)initWithFrame:(CGRect)frame{
   self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLanguege) name:kLanguage_change object:nil];
    }
    return self;
}

#pragma mark -- UI
- (void)setUI{
    
    UIImage *imgBg = [UIImage imageNamed:@"tabbar_bg_icon"];
    [imgBg stretchableImageWithLeftCapWidth:imgBg.size.width*0.5 topCapHeight:imgBg.size.height*0.5];
    if (@available(iOS 13.0, *)) {
        UITabBarAppearance *appearance = [self.standardAppearance copy];
        appearance.backgroundColor = [SUIUtil colorOf:STheme.H_D9];
        appearance.shadowColor = [SUIUtil colorOf:0xe8e8e8];
        appearance.backgroundImage = imgBg;
        appearance.shadowImage = [self lineImage];
        appearance.stackedLayoutAppearance.normal.titleTextAttributes = @{NSForegroundColorAttributeName:[SUIUtil colorOf:0x666666],NSFontAttributeName:[UIFont systemFontOfSize:12]};
        appearance.stackedLayoutAppearance.selected.titleTextAttributes = @{NSForegroundColorAttributeName:[SUIUtil colorOf:0x831CB5],NSFontAttributeName:[UIFont systemFontOfSize:12]};
        self.standardAppearance = appearance;
        if (@available(iOS 15.0, *)) {
            self.scrollEdgeAppearance = appearance;
        }
    } else {
        self.backgroundColor = [SUIUtil colorOf:STheme.H_D9];
        [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[SUIUtil colorOf:0x666666],NSFontAttributeName:[UIFont systemFontOfSize:12]} forState:UIControlStateNormal];
        [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[SUIUtil colorOf:0x831CB5],NSFontAttributeName:[UIFont systemFontOfSize:12]} forState:UIControlStateSelected];
        [self setShadowImage:[self lineImage]];
        [self setBackgroundImage:imgBg];
    }
    self.layer.shadowColor = [SUIUtil colorOf:0x000000].CGColor;
    self.layer.shadowOpacity = 0.02;
    
}
- (void)layoutSubviews{
    [super layoutSubviews];
    Class class = NSClassFromString(@"UITabBarButton");
    int btnIndex = 0;
    for (UIView *item in self.subviews){
        if ([item isKindOfClass:class]) {
            if (btnIndex == 1) { // btnIndex == 1 的时候， 为中间按钮， 添加一个背景图片
                CGFloat space = (item.width - 76)*0.5;
                self.imgvBg.frame = CGRectMake( space, -19.5, item.width-space*2, item.height + 19.5);
                [item insertSubview:self.imgvBg atIndex:0];
                self.vVip = item;
            }
            btnIndex++;
        }
    }
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if (self.isHidden == NO) {
        CGPoint newP = [self convertPoint:point toView:self.imgvBg];
        //判断如果这个新的点是在发布按钮身上，那么处理点击事件最合适的view就是发布按钮
        if ( [self.imgvBg pointInside:newP withEvent:event]) {
            return self.vVip;
        }else{//如果点不在发布按钮身上，直接让系统处理就可以了
            return [super hitTest:point withEvent:event];
        }
    }else {
        //tabbar隐藏了，那么说明已经push到其他的页面了，这个时候还是让系统去判断最合适的view处理就好了
        return [super hitTest:point withEvent:event];
    }
}
- (void)updateLanguege{
    NSArray *arrTitle = @[KLanguage(@"home_txt"),KLanguage(@"start_dete_txt"),KLanguage(@"mine_txt")];
    for (int i = 0; i< self.items.count; i++) {
        UITabBarItem *item = self.items[i];
        item.title = arrTitle[i];
    }
}

#pragma mark -- setter and getter
- (UIImage *)lineImage{
    CGRect rect = CGRectMake(0, 0, SCREEN_WIDTH, [SFitUtil fitLine]);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef content = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(content, [SUIUtil colorOf:0xffffff].CGColor);
    CGContextFillRect(content, rect);
    UIImage *lineImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return lineImg;
}
- (UIImageView *)imgvBg{
    if (!_imgvBg) {
        _imgvBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabbar_detection_bg_icon"]];
    }
    return _imgvBg;
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
